package org.ishequ.miduo;

import org.ishequ.miduo.R;
import org.ishequ.miduo.MiDuoActivity.ConfigHelper;
import org.ishequ.miduo.R.id;
import org.ishequ.miduo.R.layout;
import org.ishequ.miduo.R.style;
import org.ishequ.weibo.db.UserInfoDBHelper;
import org.ishequ.weibo.domain.UserInfo;

import weibo4android.Weibo;
import weibo4android.WeiboException;
import weibo4android.androidexamples.OAuthConstant;
import weibo4android.http.AccessToken;
import weibo4android.http.RequestToken;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;

public class AuthorizeActivity extends Activity {

	private static String LOG_TAG = "AuthorizeActivity";

	private RequestToken requestToken = null;
	private AccessToken accessToken = null;
	private Weibo weibo = null;

	private Dialog dialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.authorize);
		
		System.setProperty("weibo4j.oauth.consumerKey", Weibo.CONSUMER_KEY);
		System.setProperty("weibo4j.oauth.consumerSecret",
				Weibo.CONSUMER_SECRET);

		View diaView = View.inflate(this, R.layout.weibo_dialog, null);
		dialog = new Dialog(AuthorizeActivity.this, R.style.dialog);
		dialog.setContentView(diaView);
		dialog.show();

		ImageButton stratBtn = (ImageButton) diaView
				.findViewById(R.id.btn_start);
		stratBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Weibo weibo = new Weibo();
				try {
					requestToken = weibo
							.getOAuthRequestToken("weibo4android://AuthorizeActivity");
					OAuthConstant.getInstance().setRequestToken(requestToken);
					Uri uri = Uri.parse(requestToken.getAuthenticationURL()
							+ "&display=mobile");
					startActivity(new Intent(Intent.ACTION_VIEW, uri));
				} catch (WeiboException e) {
					e.printStackTrace();
				}
			}
		});
	}

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		// 在这里处理获取返回的oauth_verifier参数
		try {
			Uri uri = intent.getData();
			Log.i(LOG_TAG, "onNewIntent" + uri);
			if (uri != null) {
				requestToken = OAuthConstant.getInstance().getRequestToken();
				Log.i(LOG_TAG, uri + "-->" + requestToken);
				accessToken = requestToken.getAccessToken(uri
						.getQueryParameter("oauth_verifier"));
				OAuthConstant.getInstance().setAccessToken(accessToken);
				
				weibo = OAuthConstant.getInstance().getWeibo();
				weibo.setToken(OAuthConstant.getInstance().getToken(), OAuthConstant
						.getInstance().getTokenSecret());
				String[] args = new String[3];
				args[0] = OAuthConstant.getInstance().getToken();
				args[1] = OAuthConstant.getInstance().getTokenSecret();
				args[2] = String.valueOf(weibo.verifyCredentials().getId());
				
				
				UserInfo user = new UserInfo();
				user.setUserId(args[2]);
				user.setToken(args[0]);
				user.setTokenSecret(args[1]);
				
				UserInfoDBHelper helper = UserInfoDBHelper.getInstance(this);
				String uid = user.getUserId();
				if (helper.haveUserInfo(uid)) {
					helper.updateUserInfo(user);
					Log.i("UserInfo", "update");
				} else {
					helper.saveUserInfo(user);
					Log.i("UserInfo", "add");
				}
				ConfigHelper.nowUser = user;
			}
		} catch (WeiboException e) {
			e.printStackTrace();
		}

	}

}
